﻿
Imports std
Imports elpmis

Namespace httpFunctions
    Public MustInherit Class formdatafunc(Of OT)
        Inherits inputIFunction(Of formdata, OT)

        Protected Sub New(ByVal name As String, ByVal a As aliases)
            MyBase.New(name, a)
        End Sub

        Protected Sub New(ByVal name As String)
            Me.New(name, Nothing)
        End Sub

        Protected NotOverridable Overrides Function finder() As iVariableFinder(Of formdata)
            Return formdata_finder()
        End Function
    End Class

    Public Class formdataname
        Inherits formdatafunc(Of String)

        Public Sub New()
            MyBase.New("formdata.name")
        End Sub

        Protected Overrides Function invoke(ByVal i As formdata, ByRef o As String) As Boolean
            copy(o, i.name())
            Return True
        End Function

        Protected Overrides Function returnType() As typeDefination
            Return typeDefination.string
        End Function
    End Class

    Public Class formdatafilename
        Inherits formdatafunc(Of String)

        Public Sub New()
            MyBase.New("formdata.filename")
        End Sub

        Protected Overrides Function invoke(ByVal i As formdata, ByRef o As String) As Boolean
            copy(o, i.filename())
            Return True
        End Function

        Protected Overrides Function returnType() As typeDefination
            Return typeDefination.string
        End Function
    End Class

    Public Class formdatacontent
        Inherits formdatafunc(Of String)

        Public Sub New()
            MyBase.New("formdata.content")
        End Sub

        Protected Overrides Function invoke(ByVal i As formdata, ByRef o As String) As Boolean
            copy(o, i.content())
            Return True
        End Function

        Protected Overrides Function returnType() As typeDefination
            Return typeDefination.string
        End Function
    End Class

    Public Class formdataread
        Inherits inputIOutputOFunction(Of String, Object)

        Public Sub New()
            MyBase.New("formdata.readfile")
        End Sub

        Protected Overrides Function finder() As iVariableFinder(Of String)
            Return string_finder()
        End Function

        Protected Overrides Function invoke(ByVal i As String, ByRef o As Object) As Boolean
            Try
                o = New bytes(IO.File.ReadAllBytes(i))
                Return True
            Catch ex As Exception
                raiseError("cannot read file " + i + ", ex " + ex.Message, errorHandle.errorType.warning)
                Return False
            End Try
        End Function
    End Class

    Public Class formdatacontainsfile
        Inherits formdatafunc(Of Boolean)

        Public Sub New()
            MyBase.New("formdata.containsfile")
        End Sub

        Protected Overrides Function invoke(ByVal i As formdata, ByRef o As Boolean) As Boolean
            o = i.containsfile()
            Return True
        End Function

        Protected Overrides Function returnType() As typeDefination
            Return typeDefination.bool
        End Function
    End Class

    Public Class formdatatempfile
        Inherits formdatafunc(Of String)

        Public Sub New()
            MyBase.New("formdata.tempfilename")
        End Sub

        Protected Overrides Function invoke(ByVal i As formdata, ByRef o As String) As Boolean
            copy(o, i.tempfilename())
            Return True
        End Function

        Protected Overrides Function returnType() As typeDefination
            Return typeDefination.string
        End Function
    End Class
End Namespace
